/*
 * =====================================================================================
 *
 *       Filename:  main.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  07/07/2018 10:45:42 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include "threadpool.h"
#include "thread.h"
#include <iostream>
#include <list>

using namespace std;

class Test : public Runnable
{
public:
    Test(int id):
        m_id(id)
    {}
    virtual void run()
    {
        Thread::sleep(2000);
        cout<<m_id<<endl;
    }

    int     m_id;
};

int main(int argc, char* argv[])
{
    ThreadPool& pool = ThreadPool::defaultPool();
    list<Test*> tasks;
    for(int i = 0; i < 20; ++i)
    {
        Test* ptask = new Test(i);
        tasks.push_back(ptask);
        cout<<"pool available thread:"<<pool.available()<<endl;
        cout<<"pool working thread:"<<pool.working()<<endl;
        while (!pool.start(*ptask))
        {
            cout<<"..."<<endl;
            Thread::sleep(100);
            continue;
        }
    }
    cout<<"pool available thread:"<<pool.available()<<endl;
    cout<<"pool working thread:"<<pool.working()<<endl;
    
    while(pool.working() != 0)
    {
        pool.joinAll();
    }

    for(list<Test*>::iterator it = tasks.begin(); it != tasks.end(); ++it)
        delete *it;

    return 0;
}







